package com.brainydroid.items;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.brainydroid.framework.Object;
import com.brainydroid.framework.Scene;


public class CircularButton extends Object {

	int color_;
	float r_;
	Pixmap p_;
	Texture tex_;
	TextureRegion texreg_;
	
	  
	
	public CircularButton() {
	}

	
	public CircularButton(float x, float y, float r, int color) {
		touchable_ = true;
		setPosition(x,y);
		r_ = r;
		setColor(color);

		
	      p_ = new Pixmap (800, 480, Pixmap.Format.RGBA8888); // Pixmap.Format.RGBA8888);
		
		   p_.setColor(1.0f, 1.0f, 1.0f, 1.0f);    // White
		   p_.drawCircle((int)r_, (int)r_, (int)r_);
		   //  p_.setColor(0.0f, 0.0f, 1.0f, 1.0f);   // Blue
		  //    p_.drawLine(100, 100, 200, 0);
		 
		   
	      tex_ = new Texture (1024, 1024, Pixmap.Format.RGBA8888); //Pixmap.Format.RGBA8888); 	      
	      tex_.setFilter (Texture.TextureFilter.Nearest, Texture.TextureFilter.Linear);
	      tex_.setWrap   (Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);

	      tex_.draw (p_, 0,0);
	      texreg_ = new TextureRegion(tex_, 0, 0,(int)(2*r+1),(int)(2*r+1));// 2*r_, 2*r_);
	      
	      
		///////////////////
	      /*pixmap = new Pixmap (800, 480, Pixmap.Format.RGBA8888); // Pixmap.Format.RGBA8888);

	      // Create a texture to contain the pixmap
	      texture = new Texture (1024, 1024, Pixmap.Format.RGBA8888); //Pixmap.Format.RGBA8888);   
	      texture.setFilter (Texture.TextureFilter.Nearest, Texture.TextureFilter.Linear);
	      texture.setWrap   (Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
	            
	      pixmap.setColor(1.0f, 0.0f, 0.0f, 1.0f);   // Red
	      pixmap.drawLine(0, 0, 100, 100);
	      
	      pixmap.setColor(0.0f, 0.0f, 1.0f, 1.0f);   // Blue
	      pixmap.drawLine(100, 100, 200, 0);
	      
	      pixmap.setColor(0.0f, 1.0f, 0.0f, 1.0f);    // Green
	      pixmap.drawLine(100, 0, 100, 100);
	      
	      pixmap.setColor(1.0f, 1.0f, 1.0f, 1.0f);    // White
	      pixmap.drawCircle(400, 300, 100);
	      
	      texture.draw (pixmap, 0,0);
	      region = new TextureRegion(texture, 0, 0, 800, 480);
	      */
			setDimensions(2*r_, 2*r_);
//addSingleFrame(region);		

		/////////////////
		
		
	      addSingleFrame(texreg_);		

//			setDimensions(2*r_, 2*r_);

		
	}

	
	private void setColor(int color) {
		color_ = color;		
	}

	@Override
	public boolean istouch(float x,float y,float sx, float sy){
//		double d =  Math.sqrt(Math.pow(((double)(getAbsx()*sx)-(double)x),2)+Math.pow(((double)(getAbsy()*sy)-(double)y),2));

		double d =  Math.sqrt(Math.pow(((double)(getScreenx() +r_)-(double)x),2)+Math.pow(((double)(getScreeny()+r_)-(double)y),2));
//		return d<(double)(r_);
		return d<(double)Screenr();
	}
	
	public float Screenr(){
		return r_*Scene.scalex_;

	}
	
	
	
	@Override
	protected void onTouchDown() {
		// TODO Auto-generated method stub
		touched_ = true;
		
		
	}

	@Override
	protected void onTouchUp() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onTouchLong() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onTouchDrag(float x, float y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void update(float deltatime) {
		// TODO Auto-generated method stub
		
	}

	
	
	/**
	 * Checks if button was touched. If so, return true and set button to untouched
	 * @return
	 */
	public boolean checktouched(){
		if (touched_){
			touched_ = false;
			return true;
		}else{
			return false;
		}
	}


	public void center() {
		setPosition((float)Gdx.graphics.getWidth()/2-width_/2, (float)Gdx.graphics.getHeight()/2-height_/2);
	}
	

}
